// pages/me/info.js
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

//获取应用实例
var app = getApp()
Page({
  data: {
    inputViewShow: false,
    inputViewShow1: false,
    userInfo: {},
    userData: {},
    nickName: "",
    sign:"",
    showLoading: false,
  },
  onLoad: function () {
    var that = this
    var user = wx.getStorageSync("user");
    if (user) {
      that.setData({
        userInfo: user
      });
    }
  },
  onShow: function () {
    // 生命周期函数--监听页面显示
    //获取用户信息
    this.getUserInfo();
  },
  //获取用户信息
  getUserInfo: function () {
    var that = this;
    http.ApiGet(apiUrl.UrlUserInfo, {}, function (res) {
      // console.log(res);
      if (res.result.errorCode == 1) {
        var user = res.user;
        that.setData({
          userInfo: user,
          nickName: user.nickname,
          sign: user.sign,
        });
        wx.setStorageSync("user", user);
      }
    });
  },
  doLogout: function () {
    wx.showModal({
      title: '确定退出？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定');
          // wx.clearStorage();
          wx.removeStorageSync("token");
          wx.removeStorage({
            key: 'user',
            complete: function (res) {
              // wx.setStorageSync("clearStorageTemp", true);
            }
          })
          wx.reLaunch({
            url: '../login/index'
          })
        }
      }
    })
  },
  doEditPost: function (postData) {
    var that = this;
    
    postData['appId'] = util.APPID;
    postData['openId'] = wx.getStorageSync("openId");

    http.ApiPost(apiUrl.UrlInfoEdit, postData, function (res) {
      if (res.result.errorCode == 1) {
        wx.hideToast()
        wx.showToast({
          title: '修改成功！',
          icon: 'success',
          duration: 3000
        });
        that.getUserInfo();
        setTimeout(function () {
          wx.hideToast();
        }, 1000)
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  closeInput: function () {
    this.setData({
      inputViewShow: false,
      inputViewShow1: false,
    });
  },
  nickNameInput: function (e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  signInput: function (e) {
    this.setData({
      sign: e.detail.value
    })
  },
  nickNameTap: function () {
    this.setData({
      inputViewShow: true,
    });
  },
  signTap: function () {
    this.setData({
      inputViewShow1: true,
    });
  },
  doOK: function () {
    var that = this;
    var nickName = that.data.nickName;
    if (nickName == "") {
      wx.showModal({
        title: '昵称不能为空'
      });
      return;
    }
    that.closeInput();
    that.doEditPost({
      'nickname': nickName
    });
  },
  doOK1: function () {
    var that = this;
    var sign = that.data.sign;
    if (sign == "") {
      wx.showModal({
        title: '不能为空'
      });
      return;
    }
    that.closeInput();
    that.doEditPost({
      'sign': sign
    });
  },
  chooseImage: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        for (let j = 0; j < res.tempFilePaths.length; ++j) {
          that.uploadImage(res.tempFilePaths[j], type)
        }
      }
    })
  },
  uploadImage: function (path, type) {
    var that = this
    that.showLoading();
    var postData = {};
    var token = wx.getStorageSync("token");
    postData["token"] = token;
    wx.uploadFile({
      url: apiUrl.UrlUpload,
      filePath: path,
      name: 'file',
      formData: postData,
      success: function (res) {
        that.cancelLoading();
        var resdata = JSON.parse(res.data);
        var result = resdata.result;
        if (result.errorCode == 1) {
          wx.hideToast()
          if (type == 0) {
            that.doEditPost({
              'avatar': resdata.url,
            });
          } else {
            that.doEditPost({
              'background': resdata.url,
            });
          }

        } else {
          wx.showModal({
            title: result.errorMessage
          });
        }
      },
      fail: function (err) {
        console.log(err);
        that.cancelLoading();
        wx.showModal({
          title: '上传出错！'
        });
      }
    })
  },
  showLoading: function () {
    this.setData({
      showLoading: true
    })
  },
  cancelLoading: function () {
    this.setData({
      showLoading: false
    })
  },
  radioChange: function (e) {
    var that = this;
    // console.log(e.detail.value);
    that.doEditPost({
      'font_type': e.detail.value
    });
  },
  changePwd: function() {
    console.log('跳转修改密码页面');
    wx.navigateTo({
      url: '../me/repwd'
    })
  },
})